

#include <iostream>
#include <assert.h>

#include<boost/implicit_cast.hpp>

class Test_A {
    public:
        virtual void f1() { std::cout << "in f1() " << std::endl; }
        virtual ~Test_A() {}
};

class Test_B : public Test_A {

    public:
        virtual ~Test_B() {}
};

void func(Test_A* a_ptr, Test_B* b_ptr) {

}

void test_case1() {

    float f = 0;
    //int a = (int)f; //强行转换，可能会有警告
    int b = static_cast<int>(f); //使用static_cast消除警告
    //(void)a;
    (void)b;
}

void test_case2() {
    char a = 'a';
    int i = static_cast<int>(a);
    char b = static_cast<char>(i);
    std::cout << i << "  " << b << std::endl;
}

void test_case3() {

    Test_B* test_b = new Test_B();
    Test_A* test_a = new Test_A();

    Test_A* a_ptr = static_cast<Test_A*>(test_b);
    Test_B* b_ptr = static_cast<Test_B*>(test_a);

    assert(a_ptr != nullptr);
    assert(b_ptr != nullptr);

    func(a_ptr, b_ptr);

    Test_A* aa_ptr = dynamic_cast<Test_A*>(test_b);
    
    assert(aa_ptr != nullptr);

    Test_B* bb_ptr = dynamic_cast<Test_B*>(test_a);

    assert(bb_ptr != nullptr); //assert failure

    func(aa_ptr, bb_ptr);

    delete test_b;
    delete test_a;

}

class Top{};
class MiddleA:public Top{};
class MiddleB:public Top{};
class Bottom:public MiddleA,public MiddleB{};

void Function(MiddleA& A)
{
    std::cout<<"MiddleA Function"<<std::endl;
}
void Function(MiddleB& B)
{
    std::cout<<"MiddleB Function"<<std::endl;
}

void test_case4() {

    Bottom bottom;
    //Function(static_cast<MiddleA&>(bottom)); //pass
    Function(boost::implicit_cast<MiddleA&>(bottom)); //pass
   // Function(muduo::implicit_cast<MiddleA&>(bottom)); //can not pass

}

int main() {

    //test_case1();
    //test_case2();
    //test_case3();
    test_case4();
    return 0;
} 
